/*
#Copyright (c) 2013, Philippe Bordron <philippe.bordron@gmail.com>
#
# This file is part of SIPPER.
#
# DCC is free software: you can redistribute it and/or modify
# it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DCC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU LESSER GENERAL PUBLIC LICENSE for more details.
#
# You should have received a copy of the GNU LESSER GENERAL PUBLIC LICENSE
# along with SIPPER.  If not, see <http://www.gnu.org/licenses/>
*/
package combi.wip.sipper.modules;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BiColoreInclusionChemin {

	private static final String HEADER = "node [shape=rectangle, style=filled];";
	private static final String COLORSCHEME1="/bugn9/";
	private static final String COLORSCHEME2="/orrd9/";
	private static final String ZEROCOLOR="/white";
	private static final String DEFAULTCOLOR="/gray";
	private static final String TAIL = "node [shape=plaintext];\n" +
			"Legend [label=<<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\">\n" +
			"<TR><TD COLSPAN=\"2\" BGCOLOR=\"/white\">LEGEND</TD></TR>\n" +
			"<TR><TD BGCOLOR=\"/white\">set1</TD><TD BGCOLOR=\"/white\">set2</TD></TR>" +
			"<TR><TD COLSPAN=\"2\" BGCOLOR=\""+DEFAULTCOLOR+"\">not selected</TD></TR>\n" +
			"<TR><TD BGCOLOR=\""+ZEROCOLOR+"\">[0.0..0.1]</TD><TD BGCOLOR=\""+ZEROCOLOR+"\">[0.0..0.1]</TD></TR>\n" +
			"<TR><TD BGCOLOR=\""+COLORSCHEME1+"1\">]0.1..0.2]</TD><TD BGCOLOR=\""+COLORSCHEME2+"1\">]0.1..0.2]</TD></TR>\n" +
			"<TR><TD BGCOLOR=\""+COLORSCHEME1+"2\">]0.2..0.3]</TD><TD BGCOLOR=\""+COLORSCHEME2+"2\">]0.2..0.3]</TD></TR>\n" +
			"<TR><TD BGCOLOR=\""+COLORSCHEME1+"3\">]0.3..0.4]</TD><TD BGCOLOR=\""+COLORSCHEME2+"3\">]0.3..0.4]</TD></TR>\n" +
			"<TR><TD BGCOLOR=\""+COLORSCHEME1+"4\">]0.4..0.5]</TD><TD BGCOLOR=\""+COLORSCHEME2+"4\">]0.4..0.5]</TD></TR>\n" +
			"<TR><TD BGCOLOR=\""+COLORSCHEME1+"5\">]0.5..0.6]</TD><TD BGCOLOR=\""+COLORSCHEME2+"5\">]0.5..0.6]</TD></TR>\n" +
			"<TR><TD BGCOLOR=\""+COLORSCHEME1+"6\">]0.6..0.7]</TD><TD BGCOLOR=\""+COLORSCHEME2+"6\">]0.6..0.7]</TD></TR>\n" +
			"<TR><TD BGCOLOR=\""+COLORSCHEME1+"7\">]0.7..0.8]</TD><TD BGCOLOR=\""+COLORSCHEME2+"7\">]0.7..0.8]</TD></TR>\n" +
			"<TR><TD BGCOLOR=\""+COLORSCHEME1+"8\"><FONT COLOR=\"white\">]0.8..0.9]</FONT></TD><TD BGCOLOR=\""+COLORSCHEME2+"8\"><FONT COLOR=\"white\">]0.8..0.9]</FONT></TD></TR>\n" +
			"<TR><TD BGCOLOR=\""+COLORSCHEME1+"9\"><FONT COLOR=\"white\">]0.9..1.0]</FONT></TD><TD BGCOLOR=\""+COLORSCHEME2+"9\"><FONT COLOR=\"white\">]0.9..1.0]</FONT></TD></TR>\n"+
			"</TABLE>>];\n";


	private static String tagVertexLabel = "(.*label\\s*=\\s*)(\"[^\"]+\")(.*)";
	private static String tagBegin = "(.*)(\\{)(.*)";
	private static String tagEnd = "(.*)(\\})(.*)";

	public static void main(String[] args) throws IOException {
		String fileIn = args[0];
		String fileScores1 = args[1];
		String fileScores2 = args[2];
		String fileOut = args[3];

		Map<String, Double> scores1 = loadAssociation(fileScores1);
		System.err.println(scores1);
		Map<String, Double> scores2 = loadAssociation(fileScores2);
		System.err.println(scores2);
		Pattern recoVertexLabel = Pattern.compile(tagVertexLabel);
		Pattern recoBegin = Pattern.compile(tagBegin);
		Pattern recoEnd = Pattern.compile(tagEnd);
		Matcher m;
		BufferedReader in = new BufferedReader (new FileReader (fileIn));
		BufferedWriter out = new BufferedWriter (new FileWriter (fileOut));
		String ch = null;
		while ((ch = in.readLine()) != null)
		{
			m = recoVertexLabel.matcher(ch);
			if(m.find())
			{	
				System.err.println(m.group(2));
				ch = m.group(1)+ m.group(2)+ produceString(m.group(2), scores1, scores2) +m.group(3);
			}
			m = recoBegin.matcher(ch);
			if(m.find())
			{
				ch = m.group(1)+ m.group(2)+ "\n"+ HEADER +m.group(3);
			}
			m = recoEnd.matcher(ch);
			if(m.find())
			{
				ch = m.group(1)+ "\n"+ TAIL +m.group(2)+ m.group(3);
			}
			out.write(ch+"\n");
		}
		in.close();
		out.close();
	}

	private static String selectColor(int colorIndex, String colorScheme)
	{
		if (colorIndex==0 || colorIndex==-1)
		{
			return ZEROCOLOR;
		}
		if (colorIndex<-1 || colorIndex>9)
		{
			return DEFAULTCOLOR;
		}
		else
		{
			return colorScheme+colorIndex;
		}
	}

	private static String produceString(String id, Map<String, Double> scores1, Map<String, Double> scores2) {
		String result = "";
		int i1 = -2;
		int i2 = -2;
		if(scores1.containsKey(id))
		{
			double d = scores1.get(id);
			i1 = (int) (Math.ceil(d*10))-1;
		}
		if(scores2.containsKey(id))
		{
			double d = scores2.get(id);
			i2 = (int) (Math.ceil(d*10))-1;
		}
		result += ", fillcolor = \""+selectColor(i1, COLORSCHEME1)+ ":" + selectColor(i2, COLORSCHEME2) +"\"";

		if ((i1+i2)/2>7)
		{
			result += ", fontcolor = \"white\"";
		}
		return result;
	}

	private static Map<String, Double> loadAssociation(String fileIn) throws IOException
	{
		Map<String, Double> result = new HashMap<String, Double>();
		String tag = "(\\S+)\\s+(\\S+)";
		Pattern reco = Pattern.compile(tag);
		Matcher m;
		BufferedReader in = new BufferedReader (new FileReader (fileIn));
		String ch = null;
		while ((ch = in.readLine()) != null)
		{
			m = reco.matcher(ch);
			if(m.find())
			{
				result.put(m.group(1), Double.parseDouble(m.group(2)));
			}
		}
		in.close();
		return result;
	}
}
